package com.yc.book.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.yc.common.biz.BizException;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class Utils {
    public static void writeJson(Object object, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String json = gson.toJson(object);
        out.append(json);
    }

    public static void checkEmpty(Object value, String msg) throws BizException {

        boolean bool = value == null || value instanceof String && ((String) value).trim().isEmpty();
        check(bool, msg);

    }

    public static void check(boolean b, String s) throws BizException {
        if (b == true) {
            throw new BizException(s);
        }
    }
}
